package com.luxiu.extensionlib.button

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.StateListDrawable
import android.util.AttributeSet
import com.luxiu.extensionlib.R

/**
 *  private var mCheckId = 0
 *  mBinding.group.setOnCheckedChangeListener { group, checkedId ->
 *      if(mCheckId!=0){
 *          val lastCheckButton = group.findViewById<CircleRadioButton>(mCheckId)
 *          lastCheckButton.animateSelection(false)
 *      }
 *      val thisCheckButton = group.findViewById<CircleRadioButton>(checkedId)
 *      thisCheckButton.animateSelection(true)
 *      mCheckId = checkedId
 *  }
 */
class CircleRadioButton(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : androidx.appcompat.widget.AppCompatRadioButton(context, attrs, defStyleAttr) {
    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    private var mFillColor: Int
    private var mStrokeWidth: Float
    private var mStrokeColor: Int
    var mButtonValue: Int
    var mMagnification: Float = 1.4f

    init {
        // 隐藏默认按钮
        buttonDrawable = null

        isClickable = true
        // 读取自定义属性
        context.obtainStyledAttributes(attrs, R.styleable.CircleRadioButton).apply {
            try {
                mFillColor = getColor(R.styleable.CircleRadioButton_mFillColor, Color.BLACK)
                mStrokeWidth = getDimension(R.styleable.CircleRadioButton_mStrokeWidth, 0f)
                mStrokeColor = getColor(R.styleable.CircleRadioButton_mStrokeColor, Color.TRANSPARENT)
                mButtonValue = getInt(R.styleable.CircleRadioButton_mButtonValue, 0)
            } finally {
                recycle()
            }
        }

        background = createSelectorDrawable()
    }

    private fun createSelectorDrawable(): Drawable {
        val states = arrayOf(
            intArrayOf(android.R.attr.state_checked), // 选中状态
            intArrayOf(-android.R.attr.state_checked)  // 未选中状态
        )
        val drawables = arrayOf(
            createCircleDrawable(mFillColor, mStrokeWidth, mStrokeColor),
            createCircleDrawable(mFillColor, 0f, Color.TRANSPARENT)
        )
        return StateListDrawable().apply {
            for (i in states.indices) {
                addState(states[i], drawables[i])
            }
        }
    }

    fun animateSelection(isSelected: Boolean) {
        val scale = if (isSelected) mMagnification else 1.0f // 放大1.2倍
        animate()
            .scaleX(scale)
            .scaleY(scale)
            .setDuration(200) // 动画时长200ms
            .start()
    }

    private fun createCircleDrawable(
        fillColor: Int,
        strokeWidth: Float,
        strokeColor: Int,
    ): Drawable {
        return GradientDrawable().apply {
            shape = GradientDrawable.OVAL
            setColor(fillColor)
            if (strokeWidth > 0) {
                setStroke(
                    strokeWidth.toInt(),
                    strokeColor
                )
            }
        }
    }
}